package top.abrsm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import top.abrsm.entity.UserEntity;
import top.abrsm.mapper.UserMapper;
import top.abrsm.service.IUserService;
import top.abrsm.util.StateEnum;
import top.abrsm.util.StateException;

import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 22:27 2022-11-12
 * @return
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Override
    public UserEntity querySingle (UserEntity userEntity) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<> ();
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isNotBlank ( userEntity.getId () ) ) {
            queryWrapper.eq ( "id", userEntity.getId () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getNickName () ) ) {
            queryWrapper.eq ( "nickName", userEntity.getNickName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getUserName () ) ) {
            queryWrapper.eq ( "userName", userEntity.getUserName () );
        }
        if ( StringUtils.isNotBlank ( userEntity.getPassword () ) ) {
            queryWrapper.eq ( "password", userEntity.getPassword () );
        }
        UserEntity user = baseMapper.selectOne ( queryWrapper );
        return user;
    }

    @Override
    public List<UserEntity> queryList (UserEntity userEntity) {
        if ( userEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        List<UserEntity> userEntities = baseMapper.selectList ( new QueryWrapper<> () );
        return userEntities;
    }
}
